package com.itgy.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itgy.project.common.ErrorCode;
import com.itgy.project.exception.BusinessException;
import com.itgy.project.mapper.UserInterfaceInfoMapper;
import com.itgy.project.model.entity.UserInterfaceInfo;
import com.itgy.project.service.UserInterfaceInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 仪
* @description 针对表【user_interface_info(用户调用接口关系表)】的数据库操作Service实现
* @createDate 2023-03-12 15:22:59
*/
@Service
public class UserInterfaceInfoServiceImpl extends ServiceImpl<UserInterfaceInfoMapper, UserInterfaceInfo>
    implements UserInterfaceInfoService {

    @Override
    public void validUserInterfaceInfo(UserInterfaceInfo userInterfaceInfo, boolean add) {
        if(userInterfaceInfo==null)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //创建时，所有的参数都要存在
        if(add)
        {
            if(userInterfaceInfo.getInterfaceInfoId()<=0 || userInterfaceInfo.getUserId()<=0)
            {
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户或接口不存在");
            }
        }
        if(userInterfaceInfo.getLeftNum() < 0)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"剩余次数小于零");
        }
    }

    @Override
    @Transactional
    public boolean invokeCount(long interfaceInfoId, long userId) {
        //判断
        if(interfaceInfoId <=0 || userId <=0 )
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        LambdaQueryWrapper<UserInterfaceInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(UserInterfaceInfo::getInterfaceInfoId,interfaceInfoId);
        lambdaQueryWrapper.eq(UserInterfaceInfo::getUserId,userId);

        List<UserInterfaceInfo> list = this.list(lambdaQueryWrapper);
        //首次调用的话给他分配调用次数
        if(list==null||list.size()==0)
        {
            UserInterfaceInfo userInterfaceInfo = new UserInterfaceInfo();
            userInterfaceInfo.setUserId(userId);
            userInterfaceInfo.setInterfaceInfoId(interfaceInfoId);
            userInterfaceInfo.setLeftNum(100);
            this.save(userInterfaceInfo);
        }
        UpdateWrapper<UserInterfaceInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("interface_info_id",interfaceInfoId);
        updateWrapper.eq("user_id",userId);
        updateWrapper.gt("left_num",0);
        updateWrapper.setSql("left_num = left_num - 1,total_num = total_num + 1");
        boolean update = this.update(updateWrapper);

        return update;
    }
}




